/*-
 * Status.java
 *
 * $LastChangedBy:  $
 * $LastChangedRevision:  $
 * $LastChangedDate:  $
 * $HeadURL: $
 *
 * Copyright (c) 2008, Stein Gunnar Bakkeby
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Stein Gunnar Bakkeby nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.attention;

// TODO this "Status" may need another name
/** Message storage status. */
public enum Status {
  /** Received unread messages (default) */
  RECEIVED_UNREAD_MESSAGES('0'),
  /** Received read messages */
  RECEIVED_READ_MESSAGES('1'),
  /** Stored unsent messages */
  STORED_UNSENT_MESSAGES('2'),
  /** Stored sent messages */
  STORED_SENT_MESSAGES('3'),
  /** All messages */
  ALL_MESSAGES('4'),
  /** Unknown */
  UNKNOWN('U');
  
  private char value;
  private Status(final char value) {
    this.value = value;
  }
  
  /**
   * This will return the character value of this status.
   * 
   * @return One of the characters '0' to '4'.
   */
  public char getValue() {
    return value;
  }
  
  /**
   * This will return the status based on the given char value.
   * <BR>Parameters:
   * <BR>  '0' Received unread messages (default)
   * <BR>  '1' Received read messages
   * <BR>  '2' Stored unsent messages
   * <BR>  '3' Stored sent messages
   * <BR>  '4' All messages
   * 
   * @param value One of the above values
   * @return The corresponding status
   */
  public static Status getStatus(final char value) {
    return getStatus(value - '0');
  }
  
  /**
   * This will return the status based on the given int value.
   * <BR>Parameters:
   * <BR>  0 = Received unread messages (default)
   * <BR>  1 = Received read messages
   * <BR>  2 = Stored unsent messages
   * <BR>  3 = Stored sent messages
   * <BR>  4 = All messages
   * 
   * @param value One of the above values
   * @return The corresponding status
   */
  public static Status getStatus(final int value) {
    switch (value) {
    case 0:
      return RECEIVED_UNREAD_MESSAGES;
      
    case 1:
      return RECEIVED_READ_MESSAGES;
      
    case 2:
      return STORED_UNSENT_MESSAGES;
      
    case 3:
      return STORED_SENT_MESSAGES;
      
    case 4:
      return ALL_MESSAGES;
      
    default:
      return RECEIVED_UNREAD_MESSAGES;
    }
  }
  
  /**
   * This will return the status based on the given string value. The method
   * is case insensitive and looks to see if the given string value contains
   * any of the parameters below. 
   * <BR>
   * <BR>Parameters:
   * <BR>  "unread" Received unread messages (default)
   * <BR>  "read"   Received read messages
   * <BR>  "unsent" Stored unsent messages
   * <BR>  "sent"   Stored sent messages
   * <BR>  "all"    All messages
   * 
   * @param value One of the above values
   * @return The corresponding status
   */
  public static Status getStatus(String value) {
    final Status status;
    value = value.toLowerCase();
    if (value.indexOf("unread") >= 0) {
      status = Status.RECEIVED_UNREAD_MESSAGES;
    } else if (value.indexOf("read") >= 0) {
      status = Status.RECEIVED_READ_MESSAGES;
    } else if (value.indexOf("unsent") >= 0) {
      status = Status.STORED_UNSENT_MESSAGES;
    } else if (value.indexOf("sent") >= 0) {
      status = Status.STORED_SENT_MESSAGES;
    } else {
      status = Status.ALL_MESSAGES;
    }
    return status;
  }
}
